package com.fingard.dsp.bank.directbank.ccb02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IDeposit;
import com.fingard.dsp.bank.reqrespobj.deposit.Req7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.RetAts7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.detail.RetAts7507Detail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CCB02Deposit extends CCB02Base implements IDeposit {
    @Override
    public RetAts7507DepQry d_7507_DepQry(Req7507DepQry p_req7507) throws Exception {
        RetAts7507DepQry retAts = new RetAts7507DepQry(p_req7507.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            int queryCount = 1;
            String LOCSTR1 = "";
            boolean isContinuce = true;
            while (isContinuce) {
                //赋值通用参数
                CCB02Header bankHeader = new CCB02Header();
                bankHeader.TX_CODE = "6WD102";
                bankHeader.REQUEST_SN = getQryTransID();
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("TX_INFO");
                xmlWriter.writeElementString("ACCT_NO", p_req7507.actNum);//账号
                xmlWriter.writeElementString("LOCSTR1", LOCSTR1,true);//定位串1 多页查询使用，返回报文有定位串表示还有后续数据，下次请求报文必须将上次报文返回的定位串
                xmlWriter.writeElementString("LOCSTR2", queryCount + "");//定位串2 填写查询页号，返回为当前页号
                xmlWriter.writeElementString("MAXROW", "10");//最大返回笔数 交易返回的最大记录笔数，默认为10笔。
                xmlWriter.writeElementString("PAGEDIR", "",true);//查询方向 送空即可
                xmlWriter.writeElementString("STATUS", "",true);//子账户状态 0:查询账户状态为正常的子账户;送其它值则查全部子账户
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                        String tmpRetInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                        if (tmpRetCode != null && tmpRetCode.equals("000000")) {
                            LOCSTR1 = tmpBkReader.getSimpleTextNotNull("LOCSTR1");
                            if (LOCSTR1.equals("") && tmpBkReader.getSimpleTextNotNull("LOCSTR2").equals("")) {
                                isContinuce = false;
                            }
                            int detailCount = 0;
                            while (tmpBkReader.read("/TX/TX_INFO")) {
                                detailCount++;
                                XElement readXElement = tmpBkReader.readedXElement;
                                RetAts7507Detail retAtsDetail = new RetAts7507Detail();
                                retAtsDetail.depositCode = readXElement.getSingleTextNotNull("PSBK_NO") + "-" + readXElement.getSingleTextNotNull("PSBK_NO");//存折册号+本册笔号
                                retAtsDetail.currencyID = convertCurToATS(readXElement.getSingleTextNotNull("CURR_COD"));//币别代码
                                String term = "9";
                                String DEP_PRD = readXElement.getSingleTextNotNull("DEP_PRD");//存期
                                if (DEP_PRD.equals("03")) {
                                    term = "3";
                                } else if (DEP_PRD.equals("06")) {
                                    term = "4";
                                } else if (DEP_PRD.equals("10")) {
                                    term = "5";
                                } else if (DEP_PRD.equals("20")) {
                                    term = "6";
                                } else if (DEP_PRD.equals("30")) {
                                    term = "7";
                                } else if (DEP_PRD.equals("50")) {
                                    term = "8";
                                } else if (DEP_PRD.equals("91")) {
                                    term = "0";
                                } else if (DEP_PRD.equals("97")) {
                                    term = "1";
                                }
                                retAtsDetail.terms = term;
                                retAtsDetail.endDate = Format.Date10Format.format(Format.Date8Format.parse(readXElement.getSingleTextNotNull("DUE_DT")));//到期日期 YYYYMMDD 输出一本通主账户的到期日期
                                retAtsDetail.startDate = Format.Date10Format.format(Format.Date8Format.parse(readXElement.getSingleTextNotNull("OPAC_DT")));//开户日期 YYYYMMDD T 输出一本通主账户的开户日期
                                retAtsDetail.valueDate = Format.Date10Format.format(Format.Date8Format.parse(readXElement.getSingleTextNotNull("OPAC_DT")));//开户日期 YYYYMMDD T 输出一本通主账户的开户日期
                                //未冻结且子账户状态为0，存单方可视为正常
                                //FRZ_STS      冻结状态 Char(1) F 输出已维护子账户冻结状态（0 正常;1 冻结）
                                // ACCT_STS     账户状态 Char(1) F 0正常、1结清、4待结清、5其它
                                if (readXElement.getSingleTextNotNull("FRZ_STS").equals("0") && readXElement.getSingleTextNotNull("ACCT_STS").equals("0")) {
                                    retAtsDetail.depositStates = "1";
                                }
                                retAtsDetail.noteMoney = readXElement.getSingleTextNotNull("ACTU_AMT");//实际金额 Decimal(16,2) F 输出一本通主账户的实际金额
                                retAtsDetail.interestMoney = readXElement.getSingleTextNotNull("CLSD_INT");//结清利息 Decimal(16,2) F 输出已维护的结清利息
                                retAtsDetail.rate = readXElement.getSingleTextNotNull("INTR");//利率 Decimal(11,6) F 定期存款的利率
                                retAts.writeRetDetail(retAtsDetail);
                            }
                            if (detailCount != 10) {
                                break;
                            }
                            queryCount++;
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpRetInfo;
                            break;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                        break;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                    break;
                }
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(tmpRespCode) && !retAts.hasWrittenRespCode) {
            retAts = new RetAts7507DepQry(p_req7507.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("查无存单");
        }
        retAts.writeEnd();
        return retAts;
    }
}
